package cn.tedu.wenzhang3.base.exception;


import cn.tedu.wenzhang3.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler
    public JsonResult doHandler(MethodArgumentNotValidException e){
        String message=e.getFieldError().getDefaultMessage();
        log.debug(message);
        return JsonResult.notok(message);
    }


    @ExceptionHandler
    public JsonResult doHandler(ConstraintViolationException e){
        String message=e.getMessage().split(":")[1].trim();
        log.debug(message);
        return JsonResult.notok(message);
    }
}
